package com.eu.ebpm.engine.pvm;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.eu.ebpm.engine.cmd.pvm.ForkBeforeIntoCommand;
import com.eu.ebpm.engine.entity.deploy.Mission;
import com.eu.ebpm.engine.entity.deploy.Transfer;
import com.eu.ebpm.engine.entity.runtime.TaskEntity;
import com.eu.ebpm.engine.exception.ErrorCode;
import com.eu.ebpm.engine.exception.WorkFlowException;

/**
 * @注意: 分支汇总前,有四种任务可以并行往下执行!分别是: [用户任务] [自动任务] [泳道汇总前] [子流程开始事件]
 * @author wxh
 */
@Component
public class ForkBeforeService extends PvmEngine implements EngineService {

	@Autowired
	private ForkBeforeIntoCommand forkBeforeIntoCommand;

	public void into(Mission mission, TaskEntity task, Map<String, Object> variables) {

		forkBeforeIntoCommand.execute(mission, task, variables);

		//获取并行任务列表，将任务列表往下执行
		for (Transfer transfer : mission.getTransfers()) {

			switch (transfer.getToMission().getType()) {

			case USER_TASK:
				pvmContainer.getPvmService(EngineServiceType.USER_TASK).execute(transfer.getToMission(), task, null);
				break;

			case LANE_BEFORE:
				pvmContainer.getPvmService(EngineServiceType.LANE_BEFORE).execute(transfer.getToMission(), task, null);
				break;

			case SUB_START_EVENT:
				pvmContainer.getPvmService(EngineServiceType.SUB_START_EVENT).execute(transfer.getToMission(), task, null);
				break;

			case SERVICE_TASK:
				pvmContainer.getPvmService(EngineServiceType.SERVICE_TASK).execute(transfer.getToMission(), task, variables);
				break;

			default:
				throw new WorkFlowException(ErrorCode.RU_FORK_TARGETREF_ERROR);

			}
		}
	}

}
